package com.tj.bo.user.server;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.tj.bo.user.client.UserQueryService;
import com.tj.bo.user.client.enums.UserRealFlagEnum;
import com.tj.bo.user.client.enums.UserRegisterTypeEnum;
import com.tj.bo.user.client.vo.bean.UserBaseVo;
import com.tj.bo.user.client.vo.output.UserAccessInfoRO;
import com.tj.bo.user.client.vo.output.UserBehaviorDeviceOutput;
import com.tj.bo.user.client.vo.output.UserBriefInfoRO;
import com.tj.bo.user.client.vo.output.UserQueryOutput;
import com.tj.bo.user.server.entity.UserAccessEntity;
import com.tj.bo.user.server.entity.UserBehaviorDeviceEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: binary
 * Date: 2020/6/11
 * Time: 上午10:08
 */
@Service
public class UserQueryServiceImpl extends UserBaseService implements UserQueryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserQueryServiceImpl.class);

    @Override
    public UserQueryOutput getUserInfoByRealToken(String realToken) {
        if (StringUtils.isEmpty(realToken)) {
            return null;
        }
        UserAccessEntity userAccessEntityReal = userAccessDao.selectUserAccessByToken(realToken);
        if (userAccessEntityReal == null) {
            return null;
        }
        if (userAccessEntityReal.getExpireDate().getTime() < new Date().getTime()) {
            LOGGER.info("token失效,userCode:{},realToken:{}", userAccessEntityReal.getUserCode(), realToken);
            return null;
        }

        //有real必然有demo 此对象原则上无需判空
        UserAccessEntity userAccessEntityDemo = userAccessDao.selectUserAccessByCustomerCode(userAccessEntityReal.getAppCode(), UserRealFlagEnum.DEMO.getType(), userAccessEntityReal.getCustomerCode());
        UserQueryOutput userQueryOutput = new UserQueryOutput();

        UserBaseVo userBaseReal = new UserBaseVo();
        BeanUtils.copyProperties(userAccessEntityReal, userBaseReal);
        userBaseReal.setUserIDCode(userAccessEntityReal.getUserAccount());

        UserBaseVo userBaseDemo = new UserBaseVo();
        BeanUtils.copyProperties(userAccessEntityDemo, userBaseDemo);
        userBaseDemo.setUserIDCode(userAccessEntityDemo.getUserAccount());

        userQueryOutput.setUserBaseReal(userBaseReal);
        userQueryOutput.setUserBaseDemo(userBaseDemo);
        userQueryOutput.setUserRegisterTypeEnum(UserRegisterTypeEnum.getEnum(userAccessEntityReal.getRegisterType()));
        return userQueryOutput;
    }

    @Override
    public UserBriefInfoRO getUserInfoByUserCode(String userCode) {
        UserBriefInfoRO userBriefInfoRO = new UserBriefInfoRO();
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
        if (userAccessEntity != null) {
            if (UserRealFlagEnum.REAL.getType().equals(userAccessEntity.getRealFlag())) {
                UserAccessEntity entity = userAccessDao.selectUserAccessByCustomerCode(userAccessEntity.getAppCode(), UserRealFlagEnum.DEMO.getType(), userAccessEntity.getCustomerCode());
                userBriefInfoRO.setDemoUserCode(entity.getUserCode());
                userBriefInfoRO.setRealUserCode(userAccessEntity.getUserCode());
                userBriefInfoRO.setRealToken(userAccessEntity.getToken());
                userBriefInfoRO.setDeviceId(entity.getDeviceId());
                userBriefInfoRO.setToken(entity.getToken());
            } else {
                UserAccessEntity entity = userAccessDao.selectUserAccessByCustomerCode(userAccessEntity.getAppCode(), UserRealFlagEnum.REAL.getType(), userAccessEntity.getCustomerCode());
                if (entity != null) {
                    userBriefInfoRO.setRealUserCode(entity.getUserCode());
                    userBriefInfoRO.setRealToken(entity.getToken());
                }
                userBriefInfoRO.setDemoUserCode(userAccessEntity.getUserCode());
                userBriefInfoRO.setDeviceId(entity != null ? entity.getDeviceId() : userAccessEntity.getDeviceId());
            }
            return userBriefInfoRO;
        } else {
            return null;
        }
    }

    @Override
    public String getTokenByUserCode(String userCode) {
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
        if (userAccessEntity != null) {
            return userAccessEntity.getToken();
        }
        return null;
    }

    /**
     * 根据设备号查虚拟用户
     *
     * @param appCode
     * @param deviceId
     * @return
     */
    @Override
    public UserQueryOutput getUserInfoByDeviceId(String appCode, String deviceId) {
        if (StringUtils.isEmpty(deviceId)) {
            return null;
        }
        UserAccessEntity userAccessEntityDemo = userAccessDao.selectUserAccessByLoginNumber(appCode, UserRealFlagEnum.DEMO.getType(), deviceId);
        if (userAccessEntityDemo == null) {
            return null;
        }
        UserQueryOutput userQueryOutput = new UserQueryOutput();

        UserBaseVo userBaseDemo = new UserBaseVo();
        BeanUtils.copyProperties(userAccessEntityDemo, userBaseDemo);
        userBaseDemo.setUserIDCode(userAccessEntityDemo.getUserAccount());

        userQueryOutput.setUserBaseReal(null);
        userQueryOutput.setUserBaseDemo(userBaseDemo);
        userQueryOutput.setUserRegisterTypeEnum(UserRegisterTypeEnum.getEnum(userAccessEntityDemo.getRegisterType()));

        return userQueryOutput;
    }

    @Override
    public UserBehaviorDeviceOutput getUserDeviceInfoByUserCode(String userCode) {
        if (userCode == null || userCode.length() == 0) {
            return null;
        }
        userAccessDao.selectUserAccessByUserCode(userCode);

        UserBehaviorDeviceEntity entity = userBehaviorDeviceDao.selectUserBehaviorDeviceByUserCode(userCode);
        if (entity == null) {
            return null;
        }
        UserBehaviorDeviceOutput output = new UserBehaviorDeviceOutput();
        BeanUtils.copyProperties(entity, output);

        return output;
    }

    @Override
    public UserAccessInfoRO getUserAccessInfo(String email) {
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByAppCodeAndEmail("BO", UserRealFlagEnum.REAL.getType(), email);
        UserAccessInfoRO userAccessInfo = null;
        if (userAccessEntity != null) {
            userAccessInfo = new UserAccessInfoRO();
            BeanUtils.copyProperties(userAccessEntity, userAccessInfo);
        }
        return userAccessInfo;
    }

    @Override
    public UserAccessInfoRO getUserAccessInfoByUserCode(String userCode) {
        UserAccessEntity entity = userAccessDao.selectUserAccessByUserCode(userCode);
        UserAccessInfoRO userAccessInfo = null;
        if(entity != null){
            userAccessInfo = new UserAccessInfoRO();
            BeanUtils.copyProperties(entity, userAccessInfo);
        }
        return userAccessInfo;
    }

    @Override
    public boolean needAuthentication(String userCode) {
        UserAccessEntity entity = userAccessDao.selectUserAccessByUserCode(userCode);
        if(entity != null && entity.getIsAuthentication() == 1
                && StringUtils.isNotEmpty(entity.getMobile())
                && StringUtils.isNotEmpty(entity.getEmail())
                && StringUtils.isNotEmpty(entity.getUserName())){
            return false;
        }
        return true;
    }

    @Override
    public List<UserAccessInfoRO> selectRealUserAccessGroupByDeviceId(Integer deviceIdNum) {
        List<UserAccessEntity> userAccessEntities = userAccessDao.selectRealUserAccessGroupByDeviceId(deviceIdNum);
        List<UserAccessInfoRO> list = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(userAccessEntities)){
            for(UserAccessEntity entity : userAccessEntities){
                UserAccessInfoRO userAccessInfoRO = new UserAccessInfoRO();
                BeanUtils.copyProperties(entity, userAccessInfoRO);
                list.add(userAccessInfoRO);
            }
        }
        return list;
    }
}
